/* Copyright 2009
 *
 * This program and the accompanying materials
 * are made available under the terms of the
 * Eclipse Public License v1.0 which accompanies
 * this distribution, and is available at
 *
 * 		http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in
 * writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
 * OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * Contributors:
 * 	   IBM Corporation - initial API and implementation for JDT/DLTK
 *     Sean W. Quinn - initial adoption for use with PHP from various sources.
 */
package org.eclipse.php.internal.ui.widgets;

import java.util.ArrayList;

import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.php.internal.ui.PHPUiPluginExt;
import org.eclipse.php.internal.ui.preferences.formatter.InnerNode;
import org.eclipse.php.internal.ui.preferences.formatter.Node;
import org.eclipse.php.internal.ui.preferences.formatter.OptionNode;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

/**
 * @author Sean W. Quinn (swquinn@gmail.com)
 */
abstract public class ElementComponent implements IElementComponent {

	private final String PREF_INNER_INDEX = PHPUiPluginExt.PLUGIN_ID + "formatter_page.white_space.java_view.inner"; //$NON-NLS-1$
	private final String PREF_OPTION_INDEX = PHPUiPluginExt.PLUGIN_ID + "formatter_page.white_space.java_view.option"; //$NON-NLS-1$

	private final ArrayList fIndexedNodeList;
	private final ArrayList fTree;

	private InnerNode fLastSelected;

	private TreeViewer fInnerViewer;
	private CheckboxTableViewer fOptionsViewer;

	private Composite fComposite;

	public ElementComponent() {
		fIndexedNodeList = new ArrayList();
		fTree = null; //new WhiteSpaceOptions().createTreeByJavaElement(fWorkingValues);
		//WhiteSpaceOptions.makeIndexForNodes(fTree, fIndexedNodeList);
	}

	public void createContents(final int numColumns, final Composite parent) {
//
//		fComposite = new Composite(parent, SWT.NONE);
//		fComposite.setLayoutData(createGridData(numColumns, GridData.HORIZONTAL_ALIGN_FILL, SWT.DEFAULT));
//		fComposite.setLayout(createGridLayout(numColumns, false));
//
//		createLabel(numColumns, fComposite, FormatterMessages.WhiteSpaceTabPage_insert_space,
//				GridData.HORIZONTAL_ALIGN_BEGINNING);
//
//		final SashForm sashForm = new SashForm(fComposite, SWT.VERTICAL);
//		sashForm.setLayoutData(createGridData(numColumns, GridData.FILL_BOTH, SWT.DEFAULT));
//
//		fInnerViewer = new TreeViewer(sashForm, SWT.SINGLE | SWT.BORDER | SWT.V_SCROLL);
//
//		fInnerViewer.setContentProvider(new ITreeContentProvider() {
//			public Object[] getElements(Object inputElement) {
//				return ((Collection) inputElement).toArray();
//			}
//
//			public Object[] getChildren(Object parentElement) {
//				final List children = ((Node) parentElement).getChildren();
//				final ArrayList innerChildren = new ArrayList();
//				for (final Iterator iter = children.iterator(); iter.hasNext();) {
//					final Object o = iter.next();
//					if (o instanceof InnerNode)
//						innerChildren.add(o);
//				}
//				return innerChildren.toArray();
//			}
//
//			public Object getParent(Object element) {
//				if (element instanceof InnerNode)
//					return ((InnerNode) element).getParent();
//				return null;
//			}
//
//			public boolean hasChildren(Object element) {
//				final List children = ((Node) element).getChildren();
//				for (final Iterator iter = children.iterator(); iter.hasNext();)
//					if (iter.next() instanceof InnerNode)
//						return true;
//				return false;
//			}
//
//			public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
//
//			public void dispose() {}
//		});
//
//		fInnerViewer.setLabelProvider(new LabelProvider());
//
//		final GridData innerGd = createGridData(numColumns, GridData.HORIZONTAL_ALIGN_FILL | GridData.FILL_VERTICAL,
//				SWT.DEFAULT);
//		innerGd.heightHint = fPixelConverter.convertHeightInCharsToPixels(3);
//		fInnerViewer.getControl().setLayoutData(innerGd);
//
//		fOptionsViewer = CheckboxTableViewer.newCheckList(sashForm, SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL);
//		fOptionsViewer.setContentProvider(new ArrayContentProvider());
//		fOptionsViewer.setLabelProvider(new LabelProvider());
//
//		final GridData optionsGd = createGridData(numColumns, GridData.HORIZONTAL_ALIGN_FILL | GridData.FILL_VERTICAL,
//				SWT.DEFAULT);
//		optionsGd.heightHint = fPixelConverter.convertHeightInCharsToPixels(3);
//		fOptionsViewer.getControl().setLayoutData(optionsGd);
//
//		fDefaultFocusManager.add(fInnerViewer.getControl());
//		fDefaultFocusManager.add(fOptionsViewer.getControl());
//
//		fInnerViewer.setInput(fTree);
	}

	public void refreshState() {
		if (fLastSelected != null) {
			innerViewerChanged(fLastSelected);
		}
	}

	public void initialize() {
		fInnerViewer.addSelectionChangedListener(this);
		fOptionsViewer.addSelectionChangedListener(this);
		fOptionsViewer.addCheckStateListener(this);
		restoreSelections();
		refreshState();
	}

	private void restoreSelections() {
		Node node;
		final int innerIndex = getValidatedIndex(PREF_INNER_INDEX);
		node = (Node) fIndexedNodeList.get(innerIndex);
		if (node instanceof InnerNode) {
			fInnerViewer.expandToLevel(node, 0);
			fInnerViewer.setSelection(new StructuredSelection(new Object[] { node }));
			fLastSelected = (InnerNode) node;
		}

		final int optionIndex = getValidatedIndex(PREF_OPTION_INDEX);
		node = (Node) fIndexedNodeList.get(optionIndex);
		if (node instanceof OptionNode) {
			fOptionsViewer.setSelection(new StructuredSelection(new Object[] { node }));
		}

	}

	private int getValidatedIndex(final String key) {
		int index = 0;
//		try {
//			index = fDialogSettings.getInt(key);
//		}
//		catch (NumberFormatException ex) {
//			index = 0;
//		}
//		if (index < 0 || index > fIndexedNodeList.size() - 1) {
//			index = 0;
//		}
		return index;
	}

	public Control getControl() {
		return fComposite;
	}

	public void selectionChanged(SelectionChangedEvent event) {
//		final IStructuredSelection selection = (IStructuredSelection) event.getSelection();
//
//		if (selection.isEmpty() || !(selection.getFirstElement() instanceof Node))
//			return;
//
//		final Node selected = (Node) selection.getFirstElement();
//
//		if (selected == null || selected == fLastSelected)
//			return;
//
//		if (event.getSource() == fInnerViewer && selected instanceof InnerNode) {
//			fLastSelected = (InnerNode) selected;
//			fDialogSettings.put(PREF_INNER_INDEX, selected.index);
//			innerViewerChanged((InnerNode) selected);
//		}
//		else if (event.getSource() == fOptionsViewer && selected instanceof OptionNode)
//			fDialogSettings.put(PREF_OPTION_INDEX, selected.index);
	}

	private void innerViewerChanged(InnerNode selectedNode) {
//
//		final List children = selectedNode.getChildren();
//
//		final ArrayList optionsChildren = new ArrayList();
//		for (final Iterator iter = children.iterator(); iter.hasNext();) {
//			final Object o = iter.next();
//			if (o instanceof OptionNode)
//				optionsChildren.add(o);
//		}
//
//		fOptionsViewer.setInput(optionsChildren.toArray());
//
//		for (final Iterator iter = optionsChildren.iterator(); iter.hasNext();) {
//			final OptionNode child = (OptionNode) iter.next();
//			fOptionsViewer.setChecked(child, child.getChecked());
//		}
//
//		fPreview.clear();
//		fPreview.addAll(selectedNode.getSnippets());
//		doUpdatePreview();
	}

	public void checkStateChanged(CheckStateChangedEvent event) {
//		final OptionNode option = (OptionNode) event.getElement();
//		if (option != null)
//			option.setChecked(event.getChecked());
//		doUpdatePreview();
//		notifyValuesModified();
	}
}
